import { Component, OnInit } from '@angular/core';
import {VACATIONS} from '../datanumber';
import { Location } from '@angular/common';
import {Globals} from '../data';

declare var $: any;
@Component({
  selector: 'app-records',
  templateUrl: './records.component.html',
  styleUrls: ['./records.component.css']
})
export class RecordsComponent implements OnInit {

  myname : string ="";
  myposition="";
  constructor(private location: Location) { }
  vacations = VACATIONS;
  ngOnInit() {

    $.ajax({
      type: 'GET',
      url: 'http://localhost:3000/login',
      dataType: 'json',
      success: callback
    });
    function callback(data) {

      this.myname = data[0].name;
      this.myposition = data[0].position;
      if(this.myposition=="员工") {
        $.ajax({
          type: 'GET',
          url: 'http://localhost:3000/vacations',
          dataType: 'json',
          data: {
            "name": this.myname,
          },
          success:   function callback(data) {
            const str = JSON.stringify(data);
            this.formdata = data;
            let s = '';
            for (let i = 0; i < data.length; i++) {
              s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].type + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
              $('#vacations').append(s);
            }
            $.ajax({
              type: 'GET',
              url: 'http://localhost:3000/login',
              dataType: 'json',
              success: callback
            });
            function callback(data) {
              this.myname = data[0].name;
              $.ajax({
                type: 'GET',
                url: 'http://localhost:3000/acvacations',
                dataType: 'json',
                data: {
                  "name": this.myname,
                },
                success: callback
              });

              function callback(data) {
                const str = JSON.stringify(data);
                // alert('success');
                this.formdata = data;
                let s = '';
                for (let i = 0; i < data.length; i++) {
                  s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].type + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                  $('#acvacations').append(s);
                }
                $.ajax({
                  type: 'GET',
                  url: 'http://localhost:3000/login',
                  dataType: 'json',
                  success: callback
                });
                function callback(data){
                  this.myname = data[0].name;
                  $.ajax({
                    type: 'GET',
                    url: 'http://localhost:3000/novacations',
                    dataType: 'json',
                    data: {
                      "name": this.myname,
                    },
                    success: callback
                  });

                  function callback(data) {

                    const str = JSON.stringify(data);
                    // alert('success');
                    this.formdata = data;
                    let s = '';
                    for (let i = 0; i < data.length; i++) {
                      s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].type + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                      $('#novacations').append(s);
                    }
                    $.ajax({
                      type: 'GET',
                      url: 'http://localhost:3000/login',
                      dataType: 'json',
                      success: callback
                    });
                    function callback(data) {
                      this.myname = data[0].name;
                      $.ajax({
                        type: 'GET',
                        url: 'http://localhost:3000/leaves',
                        dataType: 'json',
                        data: {
                          "name": this.myname,
                        },
                        success: callback
                      });

                      function callback(data) {
                        const str = JSON.stringify(data);
                        // alert('success');
                        this.formdata = data;
                        let s = '';
                        for (let i = 0; i < data.length; i++) {
                          s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                          $('#leaves').append(s);
                        }
                        $.ajax({
                          type: 'GET',
                          url: 'http://localhost:3000/login',
                          dataType: 'json',
                          success: callback
                        });
                        function callback(data) {
                          this.myname = data[0].name;
                          $.ajax({
                            type: 'GET',
                            url: 'http://localhost:3000/acleaves',
                            dataType: 'json',
                            data: {
                              "name": this.myname,
                            },
                            success: callback
                          });

                          function callback(data) {
                            const str = JSON.stringify(data);
                            // alert('success');
                            this.formdata = data;
                            let s = '';
                            for (let i = 0; i < data.length; i++) {
                              s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                              $('#acleaves').append(s);
                            }
                            $.ajax({
                              type: 'GET',
                              url: 'http://localhost:3000/login',
                              dataType: 'json',
                              success: callback
                            });
                            function callback(data){
                              this.myname = data[0].name;
                              $.ajax({
                                type: 'GET',
                                url: 'http://localhost:3000/noleaves',
                                dataType: 'json',
                                data: {
                                  "name": this.myname,
                                },
                                success: callback
                              });

                              function callback(data) {
                                const str = JSON.stringify(data);
                                // alert('success');
                                this.formdata = data;
                                let s = '';
                                for (let i = 0; i < data.length; i++) {
                                  s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                                  $('#noleaves').append(s);
                                }

                              }
                            }

                          }
                        }

                      }
                    }

                  }
                }

              }
            }

          }
        });



      }
      else  if(this.myposition=="管理员"){
        $.ajax({
          type: 'GET',
          url: 'http://localhost:3000/vacations',
          dataType: 'json',
          success:      function callback(data) {
            const str = JSON.stringify(data);
            this.formdata = data;
            let s = '';
            for (let i = 0; i < data.length; i++) {
              s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].type + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
              $('#vacations').append(s);}

            $.ajax({
              type: 'GET',
              url: 'http://localhost:3000/acvacations',
              dataType: 'json',
              success: callback
            });
            function callback(data) {
              const str = JSON.stringify(data);
              // alert('success');
              this.formdata = data;
              let s = '';
              for (let i = 0; i < data.length; i++) {
                s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].type + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                $('#acvacations').append(s);}
              $.ajax({
                type: 'GET',
                url: 'http://localhost:3000/novacations',
                dataType: 'json',
                success: callback
              });
              function callback(data) {
                const str = JSON.stringify(data);
                // alert('success');
                this.formdata = data;
                let s = '';
                for (let i = 0; i < data.length; i++) {
                  s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].type + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                  $('#novacations').append(s);
                }
                $.ajax({
                  type: 'GET',
                  url: 'http://localhost:3000/leaves',
                  dataType: 'json',
                  success: callback
                });
                function callback(data) {
                  const str = JSON.stringify(data);
                  // alert('success');
                  this.formdata = data;
                  let s = '';
                  for (let i = 0; i < data.length; i++) {
                    s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                    $('#leaves').append(s);
                  }
                  $.ajax({
                    type: 'GET',
                    url: 'http://localhost:3000/acleaves',
                    dataType: 'json',
                    success: callback
                  });
                  function callback(data) {
                    const str = JSON.stringify(data);
                    // alert('success');
                    this.formdata = data;
                    let s = '';
                    for (let i = 0; i < data.length; i++) {
                      s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                      $('#acleaves').append(s);
                    }
                    $.ajax({
                      type: 'GET',
                      url: 'http://localhost:3000/noleaves',
                      dataType: 'json',
                      success: callback
                    });
                    function callback(data) {
                      const str = JSON.stringify(data);
                      // alert('success');
                      this.formdata = data;
                      let s = '';
                      for (let i = 0; i < data.length; i++) {
                        s = '<tr><td  width="100">' + data[i].id + '</td><td   width="100">' + data[i].name + '</td><td   width="100">' + data[i].department + '</td><td  width="100">' + data[i].bdate + '</td><td  width="100">' + data[i].edate + '</td><td  width="100">' + data[i].totaltime + '</td><td  width="100">' + data[i].categort + '</td><td  width="100">' + data[i].state + '</td></tr>';
                        $('#noleaves').append(s);
                      }

                    }
                  }
                }
              }
            }



          }
        });



      }
    }

  }

  showList(): void {
    const sub_menu = document.getElementById('sub_menu');
    const dis_v = sub_menu.style.display;

    if (dis_v === 'block')  {
      sub_menu.style.display = 'none';
    } else {
      sub_menu.style.display = 'block';
    }
  }

  goBack(): void {
    this.location.back();
  }
  nologin():void {
    $.ajax({

      url: "http://localhost:3000/login/" + "1",
      async: true,
      type: "delete",
      success: function () {
        alert("注销成功！")
      }
    });
  }
}

export class AppRoutingModule {}
